package com.vasthu.gui;

import java.util.*;
import javax.swing.table.*;

public class VstBillTableModel extends AbstractTableModel {

    private static final String[] header = {"Item Code", "Name", "Category", "Qty", "Unit Price", "Discount %", "Tax %", "Sub Total"};
    private static final Class[] colClass = {String.class, String.class, String.class, Float.class, Float.class, Float.class, Float.class, Float.class};
	
	/** item list */
	private List<VstTableItemSelectable> itemList = new ArrayList<VstTableItemSelectable>();

    public VstBillTableModel() {
    }

    @Override
    public int getRowCount() {
        return itemList.size();
    }

    @Override
    public int getColumnCount() {
        return header.length;
    }
    
    @Override
    public Class getColumnClass(int colindex) {
        return colClass[colindex];
    }
	
	@Override
    public String getColumnName(int index) {
        return header[index];
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        VstTableItemSelectable item = getObjecAtRow(rowIndex);
		if (item != null) {
			return item.getValue(columnIndex);
		}
		return null;
    }
	
	public VstTableItemSelectable getObjecAtRow(int index) {
        return itemList.get(index);
    }

	public void addItem(VstTableItemSelectable item) {
		itemList.add(item);
        // Fire table row insertion notification to table.
        fireTableRowsInserted(getRowCount() - 1, getRowCount() - 1);
	}  
	
	public void deleteRow(int row) {
		itemList.remove(row);
		fireTableRowsDeleted(row, row);
	}
	        
}